<!DOCTYPE html>

<html>
<head>
  <title>viper.js</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>viper.js</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              
            </div>
            
            <div class="content"><div class='highlight'><pre>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">var</span> root = (<span class="hljs-keyword">typeof</span> selef == <span class="hljs-string">'object'</span> &amp;&amp; selef.selef == selef &amp;&amp; selef) || (<span class="hljs-keyword">typeof</span> global == <span class="hljs-string">'object'</span> &amp;&amp; global.global == global &amp;&amp; global) || <span class="hljs-keyword">this</span> || {}
    <span class="hljs-keyword">var</span> arrayProto = <span class="hljs-built_in">Array</span>.prototype;
    <span class="hljs-keyword">var</span> push = arrayProto.push;

    <span class="hljs-keyword">var</span> viper = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">obj</span>)</span>{
        <span class="hljs-keyword">if</span>(!(<span class="hljs-keyword">this</span> <span class="hljs-keyword">instanceof</span> viper))<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> viper(obj);
        <span class="hljs-keyword">this</span>._wrapped = obj
    }
    <span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> exports !=<span class="hljs-string">'undefined'</span> &amp;&amp; !exports.nodeType){
        <span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> <span class="hljs-built_in">module</span> != <span class="hljs-string">'undefined'</span> &amp;&amp; !<span class="hljs-built_in">module</span>.nodeType &amp;&amp; <span class="hljs-built_in">module</span>.exports){
            exports = <span class="hljs-built_in">module</span>.exports = viper;
        }
        exports.viper = viper
    } <span class="hljs-keyword">else</span> {
        root.viper = viper
    }
    viper.VERSION = <span class="hljs-string">'0.1'</span>;

    <span class="hljs-keyword">var</span> MAX_ARRAY_INDEX = <span class="hljs-built_in">Math</span>.pow(<span class="hljs-number">2</span>, <span class="hljs-number">53</span>) - <span class="hljs-number">1</span>;

    <span class="hljs-keyword">var</span> isArrayLike = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">collection</span>)</span>{
        <span class="hljs-keyword">var</span> length = collection.length
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">typeof</span> length == <span class="hljs-string">'number'</span> &amp;&amp; length &gt;=<span class="hljs-number">0</span> &amp;&amp; length &lt;= MAX_ARRAY_INDEX
    }

    viper.each = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">obj, callback</span>)</span>{
        <span class="hljs-keyword">var</span> length, i = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">if</span>(isArrayLike(obj)){
            length = obj.length;
            <span class="hljs-keyword">for</span>(; i &lt; length; i++){
                <span class="hljs-keyword">if</span>(callback.call(obj[i], obj[i], i) === <span class="hljs-literal">false</span>){
                    <span class="hljs-keyword">break</span>;
                }
            }
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">for</span>(i <span class="hljs-keyword">in</span> obj){
                <span class="hljs-keyword">if</span>(callback.call(obj[i], obj[i], i) === <span class="hljs-literal">false</span>){
                    <span class="hljs-keyword">break</span>;
                }
            }
        }
        <span class="hljs-keyword">return</span> obj
    }

    viper.isFunction = <span class="hljs-function">(<span class="hljs-params">obj</span>) =&gt;</span> <span class="hljs-keyword">typeof</span> obj == <span class="hljs-string">'function'</span> || <span class="hljs-literal">false</span>

    viper.functions = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">obj</span>)</span>{
        <span class="hljs-keyword">var</span> names = []
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> key <span class="hljs-keyword">in</span> obj) {
            <span class="hljs-keyword">if</span> (viper.isFunction(obj[key])) names.push(key)
        }
        <span class="hljs-keyword">return</span> names.sort()
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <h2 id="object">Object</h2>

            </div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              
            </div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <p>数组拼接</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.ArrayConcat = <span class="hljs-function">(<span class="hljs-params">arr, ...args</span>) =&gt;</span> [].concat(arr, ...args);</pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>数组比较 返回第二个数组不包含的值</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param {数组} a 
     * @param {数组} b 
     * 
     * 
     */</span>
    viper.difference = <span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> {
        <span class="hljs-keyword">const</span> s = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>(b)
        <span class="hljs-keyword">return</span> a.filter(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> !s.has(x))
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p>数组|字符串包含 </p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param {数组或字符串} collection 
     * @param {检查的value} val 
     * @param {开始位置默认为0} formIdex 
     */</span>
    viper.includes = <span class="hljs-function">(<span class="hljs-params">collection, val, formIdex=<span class="hljs-number">0</span></span>) =&gt;</span> collection.slice(formIdex).indexOf(val) != <span class="hljs-number">-1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p>数组交集 返回两数组中同时包含的值</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param {数组} a 
     * @param {数组} b 
     */</span>
    viper.intersection = <span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> { <span class="hljs-keyword">const</span> s = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>(b); <span class="hljs-keyword">return</span> a.filter(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> s.has(x))}</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>移除数组中的元素并返回移除元素会改变原有数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param {数组} arr 
     * @param {函数} fn 三个参数 value index array 
     */</span>
    viper.remove = <span class="hljs-function">(<span class="hljs-params">arr, fn</span>) =&gt;</span> <span class="hljs-built_in">Array</span>.isArray(arr) ? arr.filter(fn).reduce(<span class="hljs-function">(<span class="hljs-params">acc, val</span>) =&gt;</span> {
        arr.splice(arr.indexOf(val), <span class="hljs-number">1</span>); 
        <span class="hljs-keyword">return</span> acc.concat(val)
    }, []) : []</pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>sample</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 随机获取数组或字符串中一个元素
     */</span>
    viper.sample = <span class="hljs-function"><span class="hljs-params">arr</span> =&gt;</span> arr[<span class="hljs-built_in">Math</span>.floor( <span class="hljs-built_in">Math</span>.random() * arr.length)]</pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p>从数组中获取n个随机元素，最大为数组的大小</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.sampleSize = <span class="hljs-function">(<span class="hljs-params">[...arr], n = <span class="hljs-number">1</span></span>) =&gt;</span> {
        <span class="hljs-keyword">let</span> m = arr.length;
        <span class="hljs-keyword">while</span>(m) {
            <span class="hljs-keyword">const</span> i = <span class="hljs-built_in">Math</span>.floor(<span class="hljs-built_in">Math</span>.random() * m--);
            [arr[m], arr[i]] = [arr[i], arr[m]]
        }
        <span class="hljs-keyword">return</span> arr.slice(<span class="hljs-number">0</span>, n)
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>chunk分割成指定大小的数组, 如果不能整分，剩余的会组成一个新的数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param {数组} arr 
     * @param {指定大小} size 
     */</span>
    viper.chunk = <span class="hljs-function">(<span class="hljs-params">arr, size</span>) =&gt;</span> <span class="hljs-built_in">Array</span>.from({<span class="hljs-attr">length</span>: <span class="hljs-built_in">Math</span>.ceil(arr.length / size)}, (v, i) =&gt;{
        <span class="hljs-keyword">return</span> arr.slice(i * size, i * size + size)
    })</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <p>删除数组中错误的值 false, null, 0, “”, undefined, and NaN</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.compact = <span class="hljs-function"><span class="hljs-params">arr</span> =&gt;</span> arr.filter(<span class="hljs-built_in">Boolean</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>统计数组中某个值出现的次数</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param {数组} arr 
     * @param {统计值} val 
     */</span>
    viper.countOccurrences = <span class="hljs-function">(<span class="hljs-params">arr, val</span>) =&gt;</span> arr.reduce(<span class="hljs-function">(<span class="hljs-params">a, v</span>)=&gt;</span>(v === val ? a + <span class="hljs-number">1</span> : a + <span class="hljs-number">0</span>), <span class="hljs-number">0</span>);</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>深度平铺数组 转换为一维数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.deepFlatten = <span class="hljs-function"><span class="hljs-params">arr</span> =&gt;</span> [].concat(...arr.map(<span class="hljs-function"><span class="hljs-params">v</span> =&gt;</span> (<span class="hljs-built_in">Array</span>.isArray(v) ? deepFlatten(v) : v)))</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <p>创建指定唯度的数组 默认为1</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.flatten = <span class="hljs-function">(<span class="hljs-params">arr, depth =<span class="hljs-number">1</span></span>) =&gt;</span> depth != <span class="hljs-number">1</span> 
        ? arr.reduce(<span class="hljs-function">(<span class="hljs-params">a, v</span>) =&gt;</span> a.concat(<span class="hljs-built_in">Array</span>.isArray(v) ? flatten(v, depth - <span class="hljs-number">1</span>) : v), [])
        : arr.reduce(<span class="hljs-function">(<span class="hljs-params">a, v</span>) =&gt;</span> a.concat(v), [])</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <p>从数组右边开始对每个元素执行指定函数</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.forEachRight = <span class="hljs-function">(<span class="hljs-params">arr, callback</span>) =&gt;</span> arr.slice(<span class="hljs-number">0</span>).reverse().forEach(callback)</pre></div></div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p>数组去重</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.distinctValuesOfArray = <span class="hljs-function"><span class="hljs-params">arr</span> =&gt;</span> [...new <span class="hljs-built_in">Set</span>(arr)]</pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>从右删除指定位置n的元素， n默认为1</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.dropRight = <span class="hljs-function">(<span class="hljs-params">arr, n = <span class="hljs-number">1</span></span>) =&gt;</span> arr.slice(<span class="hljs-number">0</span>, -n)</pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              <p>依次返回第n个元素</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.everyNth = <span class="hljs-function">(<span class="hljs-params">arr, nth</span>) =&gt;</span> arr.filter(<span class="hljs-function">(<span class="hljs-params">e, i</span>)=&gt;</span> i % nth === nth - <span class="hljs-number">1</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-20">&#182;</a>
              </div>
              <p>返回数组中唯一值</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.filterNonUnique = <span class="hljs-function"><span class="hljs-params">arr</span> =&gt;</span> arr.filter( <span class="hljs-function"><span class="hljs-params">i</span> =&gt;</span> arr.indexOf(i) === arr.lastIndexOf(i))</pre></div></div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-21">&#182;</a>
              </div>
              <p>返回数组中val出现的所有索引</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.indexOfAll = <span class="hljs-function">(<span class="hljs-params">arr, val</span>) =&gt;</span> {
        <span class="hljs-keyword">const</span> indices = []
        arr.forEach(<span class="hljs-function">(<span class="hljs-params">el, i</span>) =&gt;</span> el === val &amp;&amp; indices.push(i))
        <span class="hljs-keyword">return</span> indices
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-22">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-22">&#182;</a>
              </div>
              <p>返回数组中除去最后一个值的数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.initial = <span class="hljs-function">(<span class="hljs-params">arr</span>) =&gt;</span> arr.slice(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-23">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-23">&#182;</a>
              </div>
              <p>初始化指定范围的数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param {结束} end 
     * @param {开始 默认为0} start 
     * @param {步长 默认为1} step 
     */</span>
    viper.initializeArrayWithRange = <span class="hljs-function">(<span class="hljs-params">end, start = <span class="hljs-number">0</span>, step = <span class="hljs-number">1</span></span>) =&gt;</span> <span class="hljs-built_in">Array</span>.from({<span class="hljs-attr">length</span> : <span class="hljs-built_in">Math</span>.ceil(<span class="hljs-function">(<span class="hljs-params">end + <span class="hljs-number">1</span> -start</span>) / <span class="hljs-params">step</span>)}).<span class="hljs-params">map</span>(<span class="hljs-params">(v, i</span>)=&gt;</span> i * step + start)</pre></div></div>
            
        </li>
        
        
        <li id="section-24">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-24">&#182;</a>
              </div>
              <p>初始化指定长度并填充初始值</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.initializeArrayWithValues = <span class="hljs-function">(<span class="hljs-params">n, val = <span class="hljs-number">0</span></span>) =&gt;</span> <span class="hljs-built_in">Array</span>(n).fill(val)</pre></div></div>
            
        </li>
        
        
        <li id="section-25">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-25">&#182;</a>
              </div>
              <p>是否排序 正序返回1 降序返回-1 未排序返回0</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.isSorted = <span class="hljs-function"><span class="hljs-params">arr</span> =&gt;</span> {
        <span class="hljs-keyword">const</span> direction = arr[<span class="hljs-number">0</span>] &gt; arr[<span class="hljs-number">1</span>] ? <span class="hljs-number">-1</span> : <span class="hljs-number">1</span>;
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> [i, val] <span class="hljs-keyword">of</span> arr.entries()){
            <span class="hljs-keyword">if</span>(i === arr.length - <span class="hljs-number">1</span>){
                <span class="hljs-keyword">return</span> direction
            } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>((val - arr[i + <span class="hljs-number">1</span>]) * direction &gt; <span class="hljs-number">0</span>){
                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
            }
        }
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-26">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-26">&#182;</a>
              </div>
              <p>使用分隔符和结束符拼接所有数组中的元素并返回拼接好的字符串</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param {数组} arr 
     * @param {分隔符 默认为,} separator 
     * @param {结束符 默认和分隔符相同} end 
     */</span>
    viper.join = <span class="hljs-function">(<span class="hljs-params">arr, separator = <span class="hljs-string">','</span>, end = separator</span>) =&gt;</span> arr.reduce(<span class="hljs-function">(<span class="hljs-params">acc, val, i</span>) =&gt;</span> i == arr.length - <span class="hljs-number">2</span> ? acc + val + end : i == arr.length - <span class="hljs-number">1</span> ? acc + val : acc + val + separator, <span class="hljs-string">''</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-27">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-27">&#182;</a>
              </div>
              <p> 返回最后一个元素</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.last = <span class="hljs-function"><span class="hljs-params">arr</span> =&gt;</span> arr[arr.length - <span class="hljs-number">1</span>];</pre></div></div>
            
        </li>
        
        
        <li id="section-28">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-28">&#182;</a>
              </div>
              <p>返回多个可迭代对象中长度最长的一个</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.longestItem = <span class="hljs-function">(<span class="hljs-params">...vals</span>) =&gt;</span> [...vals].sort(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> b.length - a.length)[<span class="hljs-number">0</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-29">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-29">&#182;</a>
              </div>
              <p>从提供的数组中返回n个最大元素 如果n大于或等于提供的数组长度，则返回原始数组（按降序排列）</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.maxN = <span class="hljs-function">(<span class="hljs-params">arr, n = <span class="hljs-number">1</span></span>) =&gt;</span> [...arr].sort(<span class="hljs-function">(<span class="hljs-params">a,b</span>) =&gt;</span> b - a).slice(<span class="hljs-number">0</span>, n)</pre></div></div>
            
        </li>
        
        
        <li id="section-30">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-30">&#182;</a>
              </div>
              <p>从提供的数组中返回n个最小元素。如果n大于或等于提供的数组长度，则返回原始数组（按升序排序）</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.minN = <span class="hljs-function">(<span class="hljs-params">arr, n =<span class="hljs-number">1</span></span>) =&gt;</span> [...arr].sort(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a - b).slice(<span class="hljs-number">0</span>, n)</pre></div></div>
            
        </li>
        
        
        <li id="section-31">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-31">&#182;</a>
              </div>
              <p>返回数组的第n个元素</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.nthElement = <span class="hljs-function">(<span class="hljs-params">arr, n = <span class="hljs-number">0</span></span>) =&gt;</span> (n &gt; <span class="hljs-number">0</span> ? arr.slice(n , n + <span class="hljs-number">1</span>) : arr.slice(n)[<span class="hljs-number">0</span>])</pre></div></div>
            
        </li>
        
        
        <li id="section-32">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-32">&#182;</a>
              </div>
              <p>根据所提供的函数对每个元素的真实性将这些元素分成两个数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.partition = <span class="hljs-function">(<span class="hljs-params">arr, fn</span>) =&gt;</span>{
        <span class="hljs-keyword">return</span>  arr.reduce(
            <span class="hljs-function">(<span class="hljs-params">acc, val, i, arr</span>) =&gt;</span> {
                acc[fn(val, i, arr) ? <span class="hljs-number">0</span> : <span class="hljs-number">1</span>].push(val)
                <span class="hljs-keyword">return</span> acc
            },
            [[], []]
        )
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-33">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-33">&#182;</a>
              </div>
              <p>返回的过滤指定的值后的数组,改变原有数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.pull = <span class="hljs-function">(<span class="hljs-params">arr, ...args</span>) =&gt;</span> {
        <span class="hljs-keyword">let</span> argState = <span class="hljs-built_in">Array</span>.isArray(args[<span class="hljs-number">0</span>]) ? args[<span class="hljs-number">0</span>] : args;
        <span class="hljs-keyword">let</span> pulled = arr.filter(<span class="hljs-function">(<span class="hljs-params">v, i</span>) =&gt;</span> !argState.includes(v));
        arr.length = <span class="hljs-number">0</span>;
        pulled.forEach(<span class="hljs-function"><span class="hljs-params">v</span> =&gt;</span> arr.push(v))
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-34">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-34">&#182;</a>
              </div>
              <p>返回的过滤指定的索引后的数组, 返回过滤的值 改变原有数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.pullAtIndex = <span class="hljs-function">(<span class="hljs-params">arr, pullArr</span>) =&gt;</span> {
        <span class="hljs-keyword">let</span> removed = [];
        <span class="hljs-keyword">let</span> pulled = arr.map(<span class="hljs-function">(<span class="hljs-params">v, i</span>) =&gt;</span> pullArr.includes(i) ? removed.push(v) : v).filter(<span class="hljs-function">(<span class="hljs-params">v, i</span>)=&gt;</span> !pullArr.includes(i))
        arr.length = <span class="hljs-number">0</span>;
        pulled.forEach(<span class="hljs-function"><span class="hljs-params">v</span> =&gt;</span> arr.push(v))
        <span class="hljs-keyword">return</span> removed
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-35">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-35">&#182;</a>
              </div>
              <p>改变原始数组以过滤出指定的值,返回删除的元素 改变原有数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.pullAtValue = <span class="hljs-function">(<span class="hljs-params">arr, pullArr</span>) =&gt;</span> {
        <span class="hljs-keyword">let</span> removed = [],
            pushToRemove = arr.forEach(<span class="hljs-function">(<span class="hljs-params">v, i</span>)=&gt;</span> (pullArr.includes(v) ? removed.push(v) : v)),
            mutateTo = arr.filter(<span class="hljs-function">(<span class="hljs-params">v,i</span>) =&gt;</span> !pullArr.includes(v));
        arr.length = <span class="hljs-number">0</span>
        mutateTo.forEach(<span class="hljs-function"><span class="hljs-params">v</span> =&gt;</span> arr.push(v))
        <span class="hljs-keyword">return</span> removed
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-36">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-36">&#182;</a>
              </div>
              <p>根据条件过滤一个对象数组，同时过滤未指定的键</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.reducedFilter = <span class="hljs-function">(<span class="hljs-params">data, keys, fn</span>) =&gt;</span> data.filter(fn).map(<span class="hljs-function"><span class="hljs-params">el</span> =&gt;</span> keys.reduce(<span class="hljs-function">(<span class="hljs-params">acc, key</span>) =&gt;</span> {
        acc[key] = el[key]
        <span class="hljs-keyword">return</span> acc
    }, {}))</pre></div></div>
            
        </li>
        
        
        <li id="section-37">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-37">&#182;</a>
              </div>
              <p> 返回重新洗牌后的新数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.shuffle = <span class="hljs-function">(<span class="hljs-params">[...arr]</span>) =&gt;</span> {
        <span class="hljs-keyword">let</span> m = arr.length;
        <span class="hljs-keyword">while</span> (m) {
            <span class="hljs-keyword">const</span> i = <span class="hljs-built_in">Math</span>.floor(<span class="hljs-built_in">Math</span>.random() * m--);
            [arr[m], arr[i]] = [arr[i], arr[m]];
        }
        <span class="hljs-keyword">return</span> arr
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-38">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-38">&#182;</a>
              </div>
              <p>获取数组交集</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.similarity = <span class="hljs-function">(<span class="hljs-params">arr, values</span>) =&gt;</span> arr.filter(<span class="hljs-function"><span class="hljs-params">v</span> =&gt;</span> values.includes(v))</pre></div></div>
            
        </li>
        
        
        <li id="section-39">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-39">&#182;</a>
              </div>
              <p>返回两个数组之间的对称差异</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.symmetricDifference = <span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> {
        <span class="hljs-keyword">const</span> sA = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>(a), 
            sB = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>(b);
        <span class="hljs-keyword">return</span> [...a.filterx(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> !sB.has(x)), ...b.filter(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> !sA.has(x))]
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-40">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-40">&#182;</a>
              </div>
              <p>返回数组中除第一个元素外的所有元素</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.tail = <span class="hljs-function"><span class="hljs-params">arr</span> =&gt;</span> (arr.length &gt; <span class="hljs-number">1</span> ? arr.slice(<span class="hljs-number">1</span>) : arr)</pre></div></div>
            
        </li>
        
        
        <li id="section-41">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-41">&#182;</a>
              </div>
              <p>返回从头开始删除n个元素的数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.take = <span class="hljs-function">(<span class="hljs-params">arr, n = <span class="hljs-number">1</span></span>) =&gt;</span> arr.slice(<span class="hljs-number">0</span> ,n)</pre></div></div>
            
        </li>
        
        
        <li id="section-42">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-42">&#182;</a>
              </div>
              <p>返回从最后删除n个元素的数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.takeRight = <span class="hljs-function">(<span class="hljs-params">arr, n = <span class="hljs-number">1</span></span>) =&gt;</span> arr.slice(arr.length - n, arr.length)</pre></div></div>
            
        </li>
        
        
        <li id="section-43">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-43">&#182;</a>
              </div>
              <p>返回数组合集</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.union = <span class="hljs-function">(<span class="hljs-params">a ,b</span>) =&gt;</span> <span class="hljs-built_in">Array</span>.from(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([...a, ...b]))</pre></div></div>
            
        </li>
        
        
        <li id="section-44">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-44">&#182;</a>
              </div>
              <p>创建一个排除所有给定值的数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.without = <span class="hljs-function">(<span class="hljs-params">arr, ...args</span>) =&gt;</span> arr.filter(<span class="hljs-function"><span class="hljs-params">v</span> =&gt;</span> args.indexOf(v) === <span class="hljs-number">-1</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-45">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-45">&#182;</a>
              </div>
              <p>根据原始数组中的位置进行分组创建新的数组 如果参数数组的长度不一致，那么在未找到值的地方使用undefined</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.zip = <span class="hljs-function">(<span class="hljs-params">...arrays</span>) =&gt;</span> {
        <span class="hljs-keyword">const</span> maxLength = <span class="hljs-built_in">Math</span>.max(...arrays.map(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> x.length));
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">Array</span>.from({<span class="hljs-attr">length</span> : maxLength}).map(<span class="hljs-function">(<span class="hljs-params">_, i</span>) =&gt;</span> {
            <span class="hljs-keyword">return</span> <span class="hljs-built_in">Array</span>.from({<span class="hljs-attr">length</span> : arrays.length}, (_, k) =&gt; arrays[k][i])
        })
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-46">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-46">&#182;</a>
              </div>
              <p>给定一组有效的属性标识符和一个值数组，返回一个将属性关联到值的对象,未找到值的地方使用undefined</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/*
        zipObject(['a', 'b'], [1, 2, 3]); // {a: 1, b: 2}
    */</span>
    viper.zipObject = <span class="hljs-function">(<span class="hljs-params">props, values</span>) =&gt;</span> props.reduce(<span class="hljs-function">(<span class="hljs-params">obj, prop, index</span>) =&gt;</span> ((obj[prop] = values[index]), obj), {})</pre></div></div>
            
        </li>
        
        
        <li id="section-47">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-47">&#182;</a>
              </div>
              <p>求数字数组的平均数</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.average = <span class="hljs-function"><span class="hljs-params">arr</span> =&gt;</span> arr.reduce(<span class="hljs-function">(<span class="hljs-params">acc, val</span>) =&gt;</span> acc + val, <span class="hljs-number">0</span>) / arr.length</pre></div></div>
            
        </li>
        
        
        <li id="section-48">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-48">&#182;</a>
              </div>
              <h2 id="browser">Browser</h2>

            </div>
            
        </li>
        
        
        <li id="section-49">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-49">&#182;</a>
              </div>
              
            </div>
            
        </li>
        
        
        <li id="section-50">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-50">&#182;</a>
              </div>
              <p>复制字符串到剪贴板</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.copyToClipboard = <span class="hljs-function"><span class="hljs-params">str</span> =&gt;</span> {
        <span class="hljs-keyword">const</span> el = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">'textarea'</span>)
        el.value = str
        el.setAttribute(<span class="hljs-string">'readonly'</span>, <span class="hljs-string">''</span>)
        el.style.position = <span class="hljs-string">'absolute'</span>
        el.style.left =<span class="hljs-string">'-9999px'</span>
        <span class="hljs-built_in">document</span>.body.appendChild(el)

        <span class="hljs-keyword">const</span> selected = <span class="hljs-built_in">document</span>.getSelection().rangeCount &gt; <span class="hljs-number">0</span> ? <span class="hljs-built_in">document</span>.getSelection().getRangeAt(<span class="hljs-number">0</span>) : <span class="hljs-literal">false</span>

        el.select()
        <span class="hljs-built_in">document</span>.execCommand(<span class="hljs-string">'copy'</span>)
        <span class="hljs-built_in">document</span>.body.removeChild(el)
        <span class="hljs-keyword">if</span>(selected){
            <span class="hljs-built_in">document</span>.getSelection().removeAllRanges()
            <span class="hljs-built_in">document</span>.getSelection().addRange(selected)
        }
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-51">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-51">&#182;</a>
              </div>
              <p>返回当前页面的滚动位置</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.getScrollPosition = <span class="hljs-function">(<span class="hljs-params">el = <span class="hljs-built_in">window</span></span>) =&gt;</span>({
        <span class="hljs-attr">x</span>: el.pageXOffset !== <span class="hljs-literal">undefined</span> ? el.pageXOffset : el.scrollLeft,
        <span class="hljs-attr">y</span>: el.pageYOffset !== <span class="hljs-literal">undefined</span> ? el.pageYOffset : el.scrollTop
    })</pre></div></div>
            
        </li>
        
        
        <li id="section-52">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-52">&#182;</a>
              </div>
              <p>返回指定元素的CSS规则的值</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.getStyle = <span class="hljs-function">(<span class="hljs-params">el, ruleName</span>) =&gt;</span> getComputedStyle(el)[ruleName]</pre></div></div>
            
        </li>
        
        
        <li id="section-53">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-53">&#182;</a>
              </div>
              <p>元素是否含有某个类</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.hasClass = <span class="hljs-function">(<span class="hljs-params">el, className</span>) =&gt;</span> el.classList.contains(className)</pre></div></div>
            
        </li>
        
        
        <li id="section-54">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-54">&#182;</a>
              </div>
              <p>隐藏指定的所有元素</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.hide = <span class="hljs-function">(<span class="hljs-params">...el</span>) =&gt;</span> [...el].forEach( <span class="hljs-function"><span class="hljs-params">e</span> =&gt;</span> (e.style.display = <span class="hljs-string">'node'</span>))</pre></div></div>
            
        </li>
        
        
        <li id="section-55">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-55">&#182;</a>
              </div>
              <p>滚动到页面顶部</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.scrollToTop  = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
        <span class="hljs-keyword">const</span> c = <span class="hljs-built_in">document</span>.documentElement.scrollTop || <span class="hljs-built_in">document</span>.body.scrollTop
        <span class="hljs-keyword">if</span>(c &gt; <span class="hljs-number">0</span>){
            <span class="hljs-built_in">window</span>.requestAnimationFrame(scrollToTop)
            <span class="hljs-built_in">window</span>.scrollTo(<span class="hljs-number">0</span>, c - c / <span class="hljs-number">8</span>)
        }
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-56">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-56">&#182;</a>
              </div>
              <p>设置元素的css样式</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param {元素} el 
     * @param {样式名} ruleNmae 
     * @param {样式值} val 
     */</span>
    viper.steStyle = <span class="hljs-function">(<span class="hljs-params">el, ruleNmae, val</span>) =&gt;</span>{ el.style[ruleNmae] = val }</pre></div></div>
            
        </li>
        
        
        <li id="section-57">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-57">&#182;</a>
              </div>
              <p>显示所有置顶元素 参数为数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.show = <span class="hljs-function">(<span class="hljs-params">...el</span>) =&gt;</span> [...el].forEach(e.style.display = <span class="hljs-string">''</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-58">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-58">&#182;</a>
              </div>
              <p>切换元素类名</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.toggleClass = <span class="hljs-function">(<span class="hljs-params">el, className</span>) =&gt;</span> el.classList.toggleClass(className)</pre></div></div>
            
        </li>
        
        
        <li id="section-59">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-59">&#182;</a>
              </div>
              <h2 id="function-">Function 函数</h2>

            </div>
            
        </li>
        
        
        <li id="section-60">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-60">&#182;</a>
              </div>
              
            </div>
            
        </li>
        
        
        <li id="section-61">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-61">&#182;</a>
              </div>
              <p>执行一次函数</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.once = <span class="hljs-function"><span class="hljs-params">fn</span> =&gt;</span> {
        <span class="hljs-keyword">let</span> called = <span class="hljs-literal">false</span>;
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">...args</span>)</span>{
            <span class="hljs-keyword">if</span>(called) <span class="hljs-keyword">return</span>
            called = <span class="hljs-literal">true</span>
            <span class="hljs-keyword">return</span> fn.call(<span class="hljs-keyword">this</span>, args)
        }
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-62">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-62">&#182;</a>
              </div>
              <p>防抖</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param { 函数 } func 
     * @param { 时间 } wait 
     * @param { 是否立即执行 } immediate 
     */</span>
    viper.debounce = <span class="hljs-function">(<span class="hljs-params">func, wait, immediate</span>) =&gt;</span>{
        <span class="hljs-keyword">var</span> timeout, result;

        <span class="hljs-keyword">var</span> debounced = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">var</span> context = <span class="hljs-keyword">this</span>;
            <span class="hljs-keyword">var</span> args = <span class="hljs-built_in">arguments</span>;

            <span class="hljs-keyword">if</span> (timeout) clearTimeout(timeout);
            <span class="hljs-keyword">if</span> (immediate) {
                <span class="hljs-keyword">var</span> callNow = !timeout;
                timeout = setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
                    timeout = <span class="hljs-literal">null</span>;
                }, wait)
                <span class="hljs-keyword">if</span> (callNow) result = func.apply(context, args)
            }
            <span class="hljs-keyword">else</span> {
                timeout = setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
                    func.apply(context, args)
                }, wait);
            }
            <span class="hljs-keyword">return</span> result;
        };

        debounced.cancel = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
            clearTimeout(timeout);
            timeout = <span class="hljs-literal">null</span>;
        };

        <span class="hljs-keyword">return</span> debounced;
    
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-63">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-63">&#182;</a>
              </div>
              <h2 id="math">Math</h2>

            </div>
            
        </li>
        
        
        <li id="section-64">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-64">&#182;</a>
              </div>
              
            </div>
            
        </li>
        
        
        <li id="section-65">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-65">&#182;</a>
              </div>
              <p>阶乘</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.factorial = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> n &lt;= <span class="hljs-number">1</span> ? <span class="hljs-number">1</span> : n * factorial(n - <span class="hljs-number">1</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-66">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-66">&#182;</a>
              </div>
              <p>最大公约数</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.gcd = <span class="hljs-function">(<span class="hljs-params">x, y</span>) =&gt;</span> !y ? x : gcd(y, x % y)</pre></div></div>
            
        </li>
        
        
        <li id="section-67">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-67">&#182;</a>
              </div>
              <p>是否在指定范围内</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.inRange = <span class="hljs-function">(<span class="hljs-params">n, start, end = <span class="hljs-literal">null</span></span>) =&gt;</span> {
        <span class="hljs-keyword">if</span>(end &amp;&amp; start &gt; end) end = [start, (start = end)][<span class="hljs-number">0</span>]
        <span class="hljs-keyword">return</span> end == <span class="hljs-literal">null</span> ? n &gt;= <span class="hljs-number">0</span> &amp;&amp; n &lt; start : n &gt;= start &amp;&amp; n &lt; end 

    }</pre></div></div>
            
        </li>
        
        
        <li id="section-68">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-68">&#182;</a>
              </div>
              <p>数值2能否否能整除数值1</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.isDivisible = <span class="hljs-function">(<span class="hljs-params">first, second</span>) =&gt;</span> second % first === <span class="hljs-number">0</span></pre></div></div>
            
        </li>
        
        
        <li id="section-69">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-69">&#182;</a>
              </div>
              <p>随机获取指定范围内的整数</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.randomIntegerInRange = <span class="hljs-function">(<span class="hljs-params">min, max</span>) =&gt;</span> <span class="hljs-built_in">Math</span>.floor(<span class="hljs-built_in">Math</span>.random() * ( max - min + <span class="hljs-number">1</span>)) + min</pre></div></div>
            
        </li>
        
        
        <li id="section-70">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-70">&#182;</a>
              </div>
              <p>随机获取指定范围内的小数</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.randomNumberInRange = <span class="hljs-function">(<span class="hljs-params">min, max</span>) =&gt;</span> <span class="hljs-built_in">Math</span>.random() * (mix - min) + min</pre></div></div>
            
        </li>
        
        
        <li id="section-71">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-71">&#182;</a>
              </div>
              <p>返回指定位数的小数，省略第二个参数 四舍五入为整数</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.round = <span class="hljs-function">(<span class="hljs-params">n, decimals=<span class="hljs-number">0</span></span>) =&gt;</span> <span class="hljs-built_in">Number</span>(<span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-built_in">Math</span>.round(<span class="hljs-string">`<span class="hljs-subst">${n}</span>e<span class="hljs-subst">${decimals}</span>`</span>)}</span>e-<span class="hljs-subst">${decimals}</span>`</span>);</pre></div></div>
            
        </li>
        
        
        <li id="section-72">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-72">&#182;</a>
              </div>
              <h2 id="object">Object</h2>

            </div>
            
        </li>
        
        
        <li id="section-73">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-73">&#182;</a>
              </div>
              
            </div>
            
        </li>
        
        
        <li id="section-74">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-74">&#182;</a>
              </div>
              <p>从对象中挑选与给定键对应的键值对</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.pick = <span class="hljs-function">(<span class="hljs-params">obj, arr</span>) =&gt;</span> arr.reduce(<span class="hljs-function">(<span class="hljs-params">acc, curr</span>)=&gt;</span> (curr <span class="hljs-keyword">in</span> obj &amp;&amp; (acc[curr] = obj[curr]), acc), {})</pre></div></div>
            
        </li>
        
        
        <li id="section-75">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-75">&#182;</a>
              </div>
              <p>删除指定属性外的其他属性</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.cleanObj = <span class="hljs-function">(<span class="hljs-params">obj, keysToKeep = [], childIndicator</span>) =&gt;</span> {
        <span class="hljs-built_in">Object</span>.keys(obj).forEach(<span class="hljs-function"><span class="hljs-params">key</span> =&gt;</span> {
            <span class="hljs-keyword">if</span>(key === childIndicator) {
                cleanObj(obj[key], keysToKeep, childIndicator)
            } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(!keysToKeep.includes(key)){
                <span class="hljs-keyword">delete</span> obj[key]
            }
        })
        <span class="hljs-keyword">return</span> obj
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-76">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-76">&#182;</a>
              </div>
              <p>反转key和value</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.invertKeyValus = <span class="hljs-function"><span class="hljs-params">obj</span> =&gt;</span> <span class="hljs-built_in">Object</span>.keys(obj).reduce(<span class="hljs-function">(<span class="hljs-params">acc, key</span>)=&gt;</span> {
        acc[obj[key]] = key
        <span class="hljs-keyword">return</span> acc
    },{})</pre></div></div>
            
        </li>
        
        
        <li id="section-77">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-77">&#182;</a>
              </div>
              <p>小写所有key</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.lowercaseKeys = <span class="hljs-function"><span class="hljs-params">obj</span> =&gt;</span> <span class="hljs-built_in">Object</span>.keys(obj).reduce(<span class="hljs-function">(<span class="hljs-params">acc, key</span>)=&gt;</span> {
        acc[key.toLowerCase()] = obj[key]
        <span class="hljs-keyword">return</span> acc
    },{})</pre></div></div>
            
        </li>
        
        
        <li id="section-78">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-78">&#182;</a>
              </div>
              <p>合并两个或者多个对象</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.merge = <span class="hljs-function">(<span class="hljs-params">...objs</span>) =&gt;</span> [...objs].reduce(
        <span class="hljs-function">(<span class="hljs-params">acc, obj</span>) =&gt;</span> <span class="hljs-built_in">Object</span>.keys(obj).reduce(
            <span class="hljs-function">(<span class="hljs-params">a, k</span>) =&gt;</span> {
                 acc[k] = acc.hasOwnProperty(k) ? [].concat(acc[k]).concat(obj[k]) : obj[k];
                 <span class="hljs-keyword">return</span> acc
            },{}),
        {}
    )</pre></div></div>
            
        </li>
        
        
        <li id="section-79">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-79">&#182;</a>
              </div>
              <p>获取对象、数组、字符串的大小</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.size = <span class="hljs-function"><span class="hljs-params">val</span> =&gt;</span> <span class="hljs-built_in">Array</span>.isArray(val) ? val.length : val &amp;&amp; <span class="hljs-keyword">typeof</span> val === <span class="hljs-string">'object'</span> ? val.size || val.length || <span class="hljs-built_in">Object</span>.keys(val).length : <span class="hljs-keyword">typeof</span> val === <span class="hljs-string">'string'</span> ? <span class="hljs-keyword">new</span> Blob([val]).size : <span class="hljs-number">0</span></pre></div></div>
            
        </li>
        
        
        <li id="section-80">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-80">&#182;</a>
              </div>
              <h2 id="string-">String 字符串</h2>

            </div>
            
        </li>
        
        
        <li id="section-81">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-81">&#182;</a>
              </div>
              
            </div>
            
        </li>
        
        
        <li id="section-82">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-82">&#182;</a>
              </div>
              <p>将数值字符串转换成数组</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>   viper.digtize = <span class="hljs-function"><span class="hljs-params">n</span> =&gt;</span> [...<span class="hljs-string">`<span class="hljs-subst">${n}</span>`</span>].map(<span class="hljs-function"><span class="hljs-params">i</span> =&gt;</span> <span class="hljs-built_in">parseInt</span>(i))</pre></div></div>
            
        </li>
        
        
        <li id="section-83">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-83">&#182;</a>
              </div>
              <p>生成一个字符串所有的排列组合</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.anagrams = <span class="hljs-function"><span class="hljs-params">str</span> =&gt;</span> {
        <span class="hljs-keyword">if</span>(str.length &lt;= <span class="hljs-number">2</span>) <span class="hljs-keyword">return</span> str.length === <span class="hljs-number">2</span> ? [str, str[<span class="hljs-number">1</span>] + str[<span class="hljs-number">0</span>]] : str
        <span class="hljs-keyword">return</span> str.split(<span class="hljs-string">''</span>).reduce(<span class="hljs-function">(<span class="hljs-params">acc, letter, i</span>) =&gt;</span> acc.concat(anagrams(str.slice(<span class="hljs-number">0</span>, i) + str.slice(i + <span class="hljs-number">1</span>)).map(<span class="hljs-function"><span class="hljs-params">val</span> =&gt;</span> letter + val)), [])
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-84">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-84">&#182;</a>
              </div>
              <p>返回字节大小</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.byteSize = <span class="hljs-function"><span class="hljs-params">str</span> =&gt;</span> <span class="hljs-keyword">new</span> Blob([val]).size</pre></div></div>
            
        </li>
        
        
        <li id="section-85">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-85">&#182;</a>
              </div>
              <p>首字母大写 </p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param { 字符串 } param0 
     * @param { 是否改变剩余字符为小写 默认为false } lowerRest 
     */</span>
    viper.Capitalizes = <span class="hljs-function">(<span class="hljs-params">[first, ...rest], lowerRest = <span class="hljs-literal">false</span></span>) =&gt;</span> first.toUpperCase() + (lowerRest ? rest.join(<span class="hljs-string">''</span>).toLowerCase() : rest.join(<span class="hljs-string">''</span>))</pre></div></div>
            
        </li>
        
        
        <li id="section-86">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-86">&#182;</a>
              </div>
              <p>大写单词的每个首字母</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.capitalizeEveryWord = <span class="hljs-function"><span class="hljs-params">str</span> =&gt;</span> str.replace(<span class="hljs-regexp">/\d[a-z]/g</span>, char =&gt; char.toUpperCase())</pre></div></div>
            
        </li>
        
        
        <li id="section-87">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-87">&#182;</a>
              </div>
              <p>首字母小写</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param { 字符串 } param0 
     * @param { 是否改变剩余字符为大写 默认为false } lowerRest 
     */</span>
    viper.Capitalizes = <span class="hljs-function">(<span class="hljs-params">[first, ...rest], lowerRest = <span class="hljs-literal">false</span></span>) =&gt;</span> first.toLowerCase() + (lowerRest ? rest.join(<span class="hljs-string">''</span>).toUpperCase() : rest.join(<span class="hljs-string">''</span>))</pre></div></div>
            
        </li>
        
        
        <li id="section-88">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-88">&#182;</a>
              </div>
              <p>转义HTML</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.escapeHTML = <span class="hljs-function"><span class="hljs-params">str</span> =&gt;</span> str.replace(
        <span class="hljs-regexp">/[&amp;&lt;&gt;'"]/g</span>,
        tag =&gt; ({
            <span class="hljs-string">'&amp;'</span>: <span class="hljs-string">'&amp;amp;'</span>,
            <span class="hljs-string">'&lt;'</span>: <span class="hljs-string">'&amp;lt;'</span>,
            <span class="hljs-string">'&gt;'</span>: <span class="hljs-string">'&amp;gt;'</span>,
            <span class="hljs-string">"'"</span>: <span class="hljs-string">'&amp;#39;'</span>,
            <span class="hljs-string">'"'</span>: <span class="hljs-string">'&amp;quot;'</span>
        })([tag] || tag)
    )</pre></div></div>
            
        </li>
        
        
        <li id="section-89">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-89">&#182;</a>
              </div>
              <p>将驼峰字符改成字符串</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/** 
     * 
     * @param { 字符串 } str 
     * @param { 分隔符 默认为 _} separator 
     */</span>
    viper.fromCamelCase = <span class="hljs-function">(<span class="hljs-params">str, separator = <span class="hljs-string">'_'</span></span>) =&gt;</span> str.replace(<span class="hljs-regexp">/([a-z\d])(A-Z)/g</span>, <span class="hljs-string">'$1'</span> + separator + <span class="hljs-string">'$2'</span>).replace(<span class="hljs-regexp">/([A-Z]+)([A-Z][a-z\d]+)/g</span>, <span class="hljs-string">'$1'</span> + separator + <span class="hljs-string">'$2'</span>).toLowerCase()</pre></div></div>
            
        </li>
        
        
        <li id="section-90">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-90">&#182;</a>
              </div>
              <p>用指定的字符替换除最后指定个字符以外的所有字符</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param { 字符 } str 
     * @param { 保留个数， 倒数默认为4 } num 
     * @param { 替换字符 默认为* } mask 
     */</span>
    viper.mask = <span class="hljs-function">(<span class="hljs-params">str, num = <span class="hljs-number">4</span>, mask = <span class="hljs-string">'*'</span></span>) =&gt;</span> {
        <span class="hljs-keyword">return</span> (<span class="hljs-string">''</span> + str).slice(<span class="hljs-number">0</span>, -num).replace(<span class="hljs-regexp">/./g</span>, mask) + (<span class="hljs-string">''</span> + str).slice(-num)
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-91">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-91">&#182;</a>
              </div>
              <p>检查回文</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.palindrome = <span class="hljs-function">(<span class="hljs-params">str</span>) =&gt;</span> {
        <span class="hljs-keyword">const</span> s = str.toLowerCase().replace(<span class="hljs-regexp">/[\W_]/g</span>, <span class="hljs-string">''</span>)
        <span class="hljs-keyword">return</span> (
            s === s.split().reverse().join(<span class="hljs-string">''</span>)
        )
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-92">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-92">&#182;</a>
              </div>
              <p>反转字符串</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.reverseString = <span class="hljs-function">(<span class="hljs-params">str</span>) =&gt;</span> [...str].reverse().join(<span class="hljs-string">''</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-93">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-93">&#182;</a>
              </div>
              <p>按字母顺序排列字符串中的字符</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.sortString = <span class="hljs-function">(<span class="hljs-params">str</span>) =&gt;</span> [...str].sort(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a.localeCompare(b)).join(<span class="hljs-string">''</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-94">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-94">&#182;</a>
              </div>
              <p>将字符串改为驼峰字符串</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.toCamelCase = <span class="hljs-function">(<span class="hljs-params">str</span>) =&gt;</span> {
        <span class="hljs-keyword">let</span> s = 
            str &amp;&amp; 
            str
            .match(<span class="hljs-regexp">/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g</span>)
            .map(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> x.slice(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>).toUpperCase() + x.slice(<span class="hljs-number">1</span>).toLowerCase())
            .join(<span class="hljs-string">''</span>);
        <span class="hljs-keyword">return</span> s.slice(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>).toLowerCase() + s.slice(<span class="hljs-number">1</span>)
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-95">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-95">&#182;</a>
              </div>
              <p>截断字符串在后面添加… </p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-comment">/**
     * 
     * @param { 字符串 } str 
     * @param { 保留的位数 大于3的时候包含...} num 
     */</span>
    viper.truncateString = <span class="hljs-function">(<span class="hljs-params">str, num</span>) =&gt;</span> {
        <span class="hljs-keyword">return</span> str.length &gt; num ? str.slice(<span class="hljs-number">0</span>, num &gt; <span class="hljs-number">3</span> ? num <span class="hljs-number">-3</span> : num) + <span class="hljs-string">'...'</span> : str
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-96">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-96">&#182;</a>
              </div>
              <p>反转义HTML字符串</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.unescapeHTML = <span class="hljs-function">(<span class="hljs-params">str</span>) =&gt;</span> str.replace(
        <span class="hljs-regexp">/&amp;amp;|&amp;lt;|&amp;gt;|&amp;#39;|&amp;quot;/g</span>,
        tag =&gt;
        ({
            <span class="hljs-string">'&amp;amp;'</span>: <span class="hljs-string">'&amp;'</span>,
            <span class="hljs-string">'&amp;lt;'</span>: <span class="hljs-string">'&lt;'</span>,
            <span class="hljs-string">'&amp;gt;'</span>: <span class="hljs-string">'&gt;'</span>,
            <span class="hljs-string">'&amp;#39;'</span>: <span class="hljs-string">"'"</span>,
            <span class="hljs-string">'&amp;quot;'</span>: <span class="hljs-string">'"'</span>
        }[tag] || tag)
    )</pre></div></div>
            
        </li>
        
        
        <li id="section-97">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-97">&#182;</a>
              </div>
              <h2 id="type-">type 类型</h2>

            </div>
            
        </li>
        
        
        <li id="section-98">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-98">&#182;</a>
              </div>
              
            </div>
            
        </li>
        
        
        <li id="section-99">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-99">&#182;</a>
              </div>
              <p>校验字符是否是json</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.isValidJSON = <span class="hljs-function"><span class="hljs-params">obj</span> =&gt;</span> {
        <span class="hljs-keyword">try</span> {
            <span class="hljs-built_in">JSON</span>.parse(obj)
        } <span class="hljs-keyword">catch</span> (e) {
            <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
        }
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-100">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-100">&#182;</a>
              </div>
              <p>获取类型 undefined、null、NaN 直接返回 其他返回小写的构造函数的名称</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.getType = <span class="hljs-function"><span class="hljs-params">v</span> =&gt;</span> v !== v ? <span class="hljs-string">'NaN'</span> : v === <span class="hljs-literal">undefined</span> ? <span class="hljs-string">'undefined'</span> : v === <span class="hljs-literal">null</span> ? <span class="hljs-string">'null'</span> : v.constructor.name.toLowerCase()</pre></div></div>
            
        </li>
        
        
        <li id="section-101">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-101">&#182;</a>
              </div>
              <h2 id="utility-">Utility 使用函数</h2>

            </div>
            
        </li>
        
        
        <li id="section-102">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-102">&#182;</a>
              </div>
              
            </div>
            
        </li>
        
        
        <li id="section-103">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-103">&#182;</a>
              </div>
              <p>返回第一个非null非undefined的值</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.coalesce = <span class="hljs-function">(<span class="hljs-params">...args</span>) =&gt;</span> args.find( <span class="hljs-function"><span class="hljs-params">_</span> =&gt;</span> ![<span class="hljs-literal">undefined</span>, <span class="hljs-literal">null</span>].includes(_))</pre></div></div>
            
        </li>
        
        
        <li id="section-104">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-104">&#182;</a>
              </div>
              <p>返回第一个符合过滤函数的值</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.coalesceFactory = <span class="hljs-function"><span class="hljs-params">valid</span> =&gt;</span> (...args) =&gt; args.find(valid)</pre></div></div>
            
        </li>
        
        
        <li id="section-105">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-105">&#182;</a>
              </div>
              <p>将3位数的hex颜色值转换成6为数的值</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.extendHex = <span class="hljs-function"><span class="hljs-params">shortHex</span> =&gt;</span> <span class="hljs-string">'#'</span> + shortHex.slice(shortHex.startsWith(<span class="hljs-string">'#'</span>) ? <span class="hljs-number">1</span> : <span class="hljs-number">0</span>).split(<span class="hljs-string">''</span>).map(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> x + x ).join(<span class="hljs-string">''</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-106">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-106">&#182;</a>
              </div>
              <p>返回对象包含url上的参数</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.getURLParams = <span class="hljs-function"><span class="hljs-params">url</span> =&gt;</span> url.match(<span class="hljs-regexp">/([^?=&amp;]+)(=([^&amp;]*))/g</span>).reduce(
        <span class="hljs-function">(<span class="hljs-params">a, v</span>) =&gt;</span> ((a[v.slice(<span class="hljs-number">0</span>, v.indexOf(<span class="hljs-string">'='</span>))] = v.slice(v.indexOf(<span class="hljs-string">'='</span>) + <span class="hljs-number">1</span> )),a), {}
    )</pre></div></div>
            
        </li>
        
        
        <li id="section-107">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-107">&#182;</a>
              </div>
              <p>hex转rgb</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.hexToGRB = <span class="hljs-function"><span class="hljs-params">hex</span> =&gt;</span> {
        <span class="hljs-keyword">let</span> alpha = <span class="hljs-literal">false</span>,
        h = hex.slice(hex.startsWith(<span class="hljs-string">'#'</span>) ? <span class="hljs-number">1</span> : <span class="hljs-number">0</span>);
        <span class="hljs-keyword">if</span> (h.length === <span class="hljs-number">3</span>) h = [...h].map(<span class="hljs-function"><span class="hljs-params">x</span> =&gt;</span> x + x).join(<span class="hljs-string">''</span>);
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (h.length === <span class="hljs-number">8</span>) alpha = <span class="hljs-literal">true</span>;
        h = <span class="hljs-built_in">parseInt</span>(h, <span class="hljs-number">16</span>);
        <span class="hljs-keyword">return</span> (
            <span class="hljs-string">'rgb'</span> +
            (alpha ? <span class="hljs-string">'a'</span> : <span class="hljs-string">''</span>) +
            <span class="hljs-string">'('</span> +
            (h &gt;&gt;&gt; (alpha ? <span class="hljs-number">24</span> : <span class="hljs-number">16</span>)) +
            <span class="hljs-string">', '</span> +
            ((h &amp; (alpha ? <span class="hljs-number">0x00ff0000</span> : <span class="hljs-number">0x00ff00</span>)) &gt;&gt;&gt; (alpha ? <span class="hljs-number">16</span> : <span class="hljs-number">8</span>)) +
            <span class="hljs-string">', '</span> +
            ((h &amp; (alpha ? <span class="hljs-number">0x0000ff00</span> : <span class="hljs-number">0x0000ff</span>)) &gt;&gt;&gt; (alpha ? <span class="hljs-number">8</span> : <span class="hljs-number">0</span>)) +
            (alpha ? <span class="hljs-string">`, <span class="hljs-subst">${h &amp; <span class="hljs-number">0x000000ff</span>}</span>`</span> : <span class="hljs-string">''</span>) +
            <span class="hljs-string">')'</span>
        );
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-108">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-108">&#182;</a>
              </div>
              <p>随机生成hex颜色值</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.randomHexCode = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
        <span class="hljs-keyword">let</span> n = ((<span class="hljs-built_in">Math</span>.random() * <span class="hljs-number">0xfffff</span>) | <span class="hljs-number">0</span>).toString(<span class="hljs-number">16</span>)
        <span class="hljs-keyword">return</span> <span class="hljs-string">'#'</span> + (n.length !== <span class="hljs-number">6</span> ? ((<span class="hljs-built_in">Math</span>.random() * <span class="hljs-number">0xf</span>) | <span class="hljs-number">0</span>).toString(<span class="hljs-number">16</span>) + n : n)
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-109">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-109">&#182;</a>
              </div>
              <p>RGB转hex色值</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.RGBToHex = <span class="hljs-function">(<span class="hljs-params">r, g, b</span>) =&gt;</span> ((r &lt;&lt; <span class="hljs-number">16</span>) + (g &lt;&lt; <span class="hljs-number">8</span>) + b).toString(<span class="hljs-number">16</span>).padStart(<span class="hljs-number">6</span>, <span class="hljs-string">'0'</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-110">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-110">&#182;</a>
              </div>
              <p> 返回函数运行时长</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.timeTaken = <span class="hljs-function"><span class="hljs-params">callback</span> =&gt;</span> {
        <span class="hljs-built_in">console</span>.time(<span class="hljs-string">'timeTaken'</span>)
        <span class="hljs-keyword">const</span> cb = callback()
        <span class="hljs-built_in">console</span>.timeEnd(<span class="hljs-string">'timeTaken'</span>)
        <span class="hljs-keyword">return</span> cb
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-111">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-111">&#182;</a>
              </div>
              <p>使用crypto API 生成UUID 符合RFC4122 版本 4</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.uuid = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
       <span class="hljs-keyword">return</span> ([<span class="hljs-number">1e7</span>] + <span class="hljs-number">-1e3</span> + <span class="hljs-number">-4e3</span> + <span class="hljs-number">-8e3</span> + <span class="hljs-number">-1e11</span>).replace(<span class="hljs-regexp">/[018]/g</span>, c =&gt;
            (c ^ crypto.getRandomValues(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Uint8Array</span>(<span class="hljs-number">1</span>))[<span class="hljs-number">0</span>] &amp; <span class="hljs-number">15</span> &gt;&gt; c / <span class="hljs-number">4</span>).toString(<span class="hljs-number">16</span>)
        )
    }</pre></div></div>
            
        </li>
        
        
        <li id="section-112">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-112">&#182;</a>
              </div>
              <p>邮箱验证</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    viper.validEmail = <span class="hljs-function"><span class="hljs-params">str</span> =&gt;</span> /^(([^<span class="xml"><span class="hljs-tag">&lt;&gt;</span>()\[\]\\.,;:\s@"]+(\.[^<span class="hljs-tag">&lt;&gt;</span>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str)

    viper.mixin = function(obj){
        viper.each(viper.functions(obj), function(name){
            var func = viper[name] = obj[name];
            viper.prototype[name] = function() {
                var args = [this._wrapped]
                push.apply(args, arguments)
                return func.apply(viper, args)
            }
        })
        return viper
    }

    viper.mixin(viper)
})()

</span></pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
